package com.itheima.tliasmanagementspringboot.controller;


import com.itheima.tliasmanagementspringboot.annotation.OperateLog;
import com.itheima.tliasmanagementspringboot.pojo.Dept;
import com.itheima.tliasmanagementspringboot.common.R;
import com.itheima.tliasmanagementspringboot.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 部门管理
 */
@RestController
@Slf4j//日志输出注解,哪里需要加哪里(类上),lombok提供
public class DeptController {

    @Autowired
    DeptService deptService;

    /**
     * 1.
     * 功能: 部门列表查询
     *
     * @return 统一的返回对象(R)
     */
    @GetMapping("/depts")
    public R queryAllDept() {
        log.info("查询部门: {}, {}", 1, 2);//一个{}就是一个占位符,写到文件中去
        return R.ok(deptService.queryAllDeptService());
    }

    /**
     * 功能: 根据id查询部门信息
     *
     * @return 统一返回对象(R)
     */
    @GetMapping("depts/{id}")
    public R queryDeptByCondition(@PathVariable Integer id) {
        log.info("根据id查询部门信息: {}", id);
        return R.ok(deptService.queryDeptByConditionService(id));
    }

    /**
     * 功能: 根据id查删除部门信息
     *
     * @return 统一返回对象(R)
     */
    @OperateLog
    @DeleteMapping("/depts/{id}")
    public R deleteDeptById(@PathVariable Integer id) {
        log.info("根据id删除部门信息: {}", id);
        deptService.deleteDeptById(id);
        return R.ok();
    }

    /**
     * 功能: 添加/插入一条部门信息
     *
     * @return 统一返回对象(R)
     */
    @OperateLog
    @PostMapping("/depts")
    public R insertDept(@RequestBody Dept dept) {
        log.info("添加/插入一条部门信息:");

        deptService.insertDeptService(dept);
        return R.ok();

    }

    /**
     * 功能:根据id更新部门信息
     *
     * @param dept :部门id
     * @return 统一返回对象R
     */
    @OperateLog
    @PutMapping("/depts")
    public R updateDeptById(@RequestBody Dept dept) {
        log.info("根据id删除部门信息: {}", dept);
        deptService.updateDeptByIdService(dept);
        return R.ok();
    }

}
